﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using ENLibraryClass;
using System.Resources;

using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.IO;

namespace uacars
{


    public partial class frmCitas : Form
    {
        public frmBusquedaAvanzada fba = null;
        public DataTable dtGlobal;
        public DataTable dtGrid;
        int idGlobal;


        const string ochnue = "8:00-9:00";
        const string nuedie = "9:00-10:00";
        const string dieonc = "10:00-11:00";
        const string oncdoc = "11:00-12:00";
        const string doctre = "12:00-13:00";
        const string trecat = "13:00-14:00";
        const string dsedsie = "16:00-17:00";
        const string dsiedsei = "17:00-18:00";
        const string dseidnue = "18:00-19:00";
        const string dnuevei = "19:00-20:00";

        public bool ValidarCampos()
        {
            epError.Clear();

            bool cliente = true, empleado = true;

            bool resultado = true;

            /*
            if (string.IsNullOrEmpty(tbxCliente.Text) || Validacion.Valida(tbxCliente.Text, Validacion.patronNumero) == false)
            {
                cliente = false;
                epError.SetError(tbxCliente, "Debe introducir un codigo cliente valido");
            }

            if (string.IsNullOrEmpty(tbxEmpleado.Text) || Validacion.Valida(tbxEmpleado.Text, Validacion.patronNumero) == false)
            {
                empleado = false;
                epError.SetError(tbxEmpleado, "Debe introducir un codigo empleado valido");
            }

            if (cliente == true && empleado == true )
                resultado = true;
            */
            return resultado;

        }



        public frmCitas(DataTable dtFrm)
        {
            dtGrid = dtFrm;



            InitializeComponent();
            groupBox2.Text = "Horarios " + monthCalendar1.TodayDate.Day + "/" + monthCalendar1.TodayDate.Month + "/" + monthCalendar1.TodayDate.Year;

            label5.Visible = false;
            label6.Visible = false;
            label7.Visible = false;
            label8.Visible = false;
            label9.Visible = false;
            label10.Visible = false;
            label11.Visible = false;
            label12.Visible = false;
            label13.Visible = false;
            label14.Visible = false;

            label5.Text = "8:00-9:00";
            t1.Text = label5.Text;
            label6.Text = "9:00-10:00";
            t2.Text = label6.Text;
            label7.Text = "10:00-11:00";
            t3.Text = label7.Text;
            label8.Text = "11:00-12:00";
            t4.Text = label8.Text;
            label9.Text = "12:00-13:00";
            t5.Text = label9.Text;
            label10.Text = "13:00-14:00";
            t6.Text = label10.Text;
            label11.Text = "16:00-17:00";
            t7.Text = label11.Text;
            label12.Text = "17:00-18:00";
            t8.Text = label12.Text;
            label13.Text = "18:00-19:00";
            t9.Text = label13.Text;
            label14.Text = "19:00-20:00";
            t10.Text = label14.Text;



        }


        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {

            //string texto;
            //groupBox2.Text="Horarios "+monthCalendar1.TodayDate.ToString();
            label5.Text = ochnue + "    " + "LIBRE";
            t1.Text = label5.Text;
            label6.Text = nuedie + "    " + "LIBRE";
            t2.Text = label6.Text;
            label7.Text = dieonc + "    " + "LIBRE";
            t3.Text = label7.Text;
            label8.Text = oncdoc + "    " + "LIBRE";
            t4.Text = label8.Text;
            label9.Text = doctre + "    " + "LIBRE";
            t5.Text = label9.Text;
            label10.Text = trecat + "    " + "LIBRE";
            t6.Text = label10.Text;
            label11.Text = dsedsie + "    " + "LIBRE";
            t7.Text = label11.Text;
            label12.Text = dsiedsei + "    " + "LIBRE";
            t8.Text = label12.Text;
            label13.Text = dseidnue + "    " + "LIBRE";
            t9.Text = label13.Text;
            label14.Text = dnuevei + "    " + "LIBRE";
            t10.Text = label14.Text;

            t1.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
            t2.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
            t3.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
            t4.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
            t5.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
            t6.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
            t7.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
            t8.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
            t9.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
            t10.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);


            t1.BorderStyle = BorderStyle.None;
            t2.BorderStyle = BorderStyle.None;
            t3.BorderStyle = BorderStyle.None;
            t4.BorderStyle = BorderStyle.None;
            t5.BorderStyle = BorderStyle.None;
            t6.BorderStyle = BorderStyle.None;
            t7.BorderStyle = BorderStyle.None;
            t8.BorderStyle = BorderStyle.None;
            t9.BorderStyle = BorderStyle.None;
            t10.BorderStyle = BorderStyle.None;

            groupBox2.Text = "Horarios " + monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year;


            string tmp = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year;
            label3.Text = tmp;

            DateTime tmpFecha;
            tmpFecha = DateTime.Parse(tmp);
            recalcularCitas(tmpFecha);
            //Form formHorarios = new Form();
            //formHorarios.StartPosition = FormStartPosition.Manual;

            //Label labelFecha = new Label();
            //labelFecha.Text = monthCalendar1.TodayDate.ToString();
            //labelFecha.Parent = formHorarios;
            //formHorarios.Opacity = .90;


            //formHorarios.Location.X = 0;
            //formHorarios.Loca

            //formHorarios.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            //formHorarios.Show();

        }

        private void buttonLimpiar_Click(object sender, EventArgs e)
        {
            LimpiarCampos();
        }

        public void LimpiarCampos()
        {
            textMotivo.Text = "";
            tbxCliente.Text = "";
            tbxEmpleado.Text = "";
            monthCalendar1.TodayDate = System.DateTime.Today;
            toolStripStatusLabel1.Text = "";
        }

        private void buttonAvanzada_Click(object sender, EventArgs e)
        {


            frmBusquedaAvanzada fBA = new frmBusquedaAvanzada();
            fBA.fcitas = this;
            fBA.MdiParent = this.MdiParent;
            this.Close();
            fBA.Show();
        }

        private void buttonNuevo_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            try
            {

                ENIdSiguiente idsiguiente = new ENIdSiguiente("citas", 0);
                int idCita = idsiguiente.BuscarIdSiguiente();

                string fechaSeleccionada = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
                string horaBuena = null;

                if (label4.Text == "8:00-9:00")
                    horaBuena = "8:00:00";
                if (label4.Text == "9:00-10:00")
                    horaBuena = "9:00:00";
                if (label4.Text == "10:00-11:00")
                    horaBuena = "10:00:00";
                if (label4.Text == "11:00-12:00")
                    horaBuena = "11:00:00";
                if (label4.Text == "12:00-13:00")
                    horaBuena = "12:00:00";
                if (label4.Text == "13:00-14:00")
                    horaBuena = "13:00:00";
                if (label4.Text == "16:00-17:00")
                    horaBuena = "16:00:00";
                if (label4.Text == "17:00-18:00")
                    horaBuena = "17:00:00";
                if (label4.Text == "18:00-19:00")
                    horaBuena = "18:00:00";
                if (label4.Text == "19:00-20:00")
                    horaBuena = "19:00:00";



                string horaSeleccionada = "01/01/1900 " + horaBuena;

                DateTime fechaSeleccion = new DateTime();
                fechaSeleccion = DateTime.Parse(fechaSeleccionada);
                DateTime horaSeleccion = new DateTime();
                horaSeleccion = DateTime.Parse(horaSeleccionada);

                DateTime dt = new DateTime();
                ENCitas nuevo = new ENCitas(idCita, fechaSeleccion, horaSeleccion, int.Parse(tbxEmpleado.Text), int.Parse(tbxCliente.Text), textMotivo.Text.ToString(), dt, dt);

                nuevo.AltaCita();
                idsiguiente.ActualizarIdSiguiente();

                dataGridView1.DataSource = nuevo.BuscarCitas();
                //string fechaSeleccionada=monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year+" 0:00:00";
                recalcularCitas(DateTime.Parse(fechaSeleccionada));
                toolStripStatusLabel1.Text = "Alta realizada";


                ficheros f = new ficheros();
                f.EscribirLog(2, 5, idCita.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al insertar la cita. Esta cita ya existe.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);

            }
        }

        private void buttonBuscar_Click(object sender, EventArgs e)
        {

            frmBusquedaAvanzada fBA = new frmBusquedaAvanzada();
            fBA.fcitas = this;
            fBA.MdiParent = this.MdiParent;
            this.Close();
            fBA.Show();

            /*
            //Primero se comprueba que haya alguno de los campos rellenos
            if (AlgunoRelleno())
            {
                try
                {
                    DataTable buscar = new DataTable();
                    ENCitas objetoAbuscar = new ENCitas();

                    //Hay que diferenciar los dos tipos de buscar
                    buscar = objetoAbuscar.BuscarCitasPor(); //FALTA RELLENAR LA LLAMADA
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error en la búsqueda.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            else //todos los campos vacíos
            {
                DataTable buscar = new DataTable();
                ENCitas objetoAbuscar = new ENCitas();

                //Hay que diferenciar los dos tipos de buscar
                buscar = objetoAbuscar.BuscarCitas();
            }
             */
        }

        private void frmCitas_Load(object sender, EventArgs e)
        {
            buttonBorrar.Enabled = false;
            buttonModificar.Enabled = false;
            buttonNuevo.Enabled = false;

            string tmp = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year;
            string tmp2 = "-------";
            label3.Text = tmp;
            label4.Text = tmp2;

            dataGridView1.DataSource = dtGrid;
            label5.Text += "    " + "LIBRE";
            t1.Text = label5.Text;
            label6.Text += "    " + "LIBRE";
            t2.Text = label6.Text;
            label7.Text += "    " + "LIBRE";
            t3.Text = label7.Text;
            label8.Text += "    " + "LIBRE";
            t4.Text = label8.Text;
            label9.Text += "    " + "LIBRE";
            t5.Text = label9.Text;
            label10.Text += "    " + "LIBRE";
            t6.Text = label10.Text;
            label11.Text += "    " + "LIBRE";
            t7.Text = label11.Text;
            label12.Text += "    " + "LIBRE";
            t8.Text = label12.Text;
            label13.Text += "    " + "LIBRE";
            t9.Text = label13.Text;
            label14.Text += "    " + "LIBRE";
            t10.Text = label14.Text;
            recalcularCitas(System.DateTime.Now);

        }


        private void recalcularCitas(DateTime d)
        {

            DataTable resultado = new DataTable();
            ENCitas cargaHoraInicio = new ENCitas();
            DateTime dt = new DateTime();
            DateTime fechaHoyEnFormato = new DateTime();
            string dtString = "01/01/1900 0:00:00";
            dt = DateTime.Parse(dtString);
            //string fechaHoy = System.DateTime.Now.ToShortDateString(); //////<----------
            string fechaHoy = d.ToString();
            //string fechaHoy = "11/11/2005";
            fechaHoyEnFormato = DateTime.Parse(fechaHoy);
            ENCitas busqueda = new ENCitas(0, fechaHoyEnFormato, dt, 0, 0, "null", dt, dt);

            resultado = busqueda.BuscarCitasPor();
            dtGlobal = resultado;

            if (resultado.Rows.Count != 0) //SI hay citas ese dia
            {
                DateTime horaCita;


                foreach (DataRow row in resultado.Rows)
                {
                    horaCita = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));




                    switch (horaCita.Hour)
                    {
                        case 8:
                            label5.Text = ochnue + "    " + "OCUPADO";
                            t1.Text = label5.Text;
                            t1.BackColor = System.Drawing.Color.FromArgb(255, 128, 128); break;

                        case (9):

                            label6.Text = nuedie + "    " + "OCUPADO";
                            t2.Text = label6.Text;
                            t2.BackColor = System.Drawing.Color.FromArgb(255, 128, 128); break;

                        case 10:

                            label7.Text = dieonc + "    " + "OCUPADO";
                            t3.Text = label7.Text;
                            t3.BackColor = System.Drawing.Color.FromArgb(255, 128, 128); break;

                        case 11:

                            label8.Text = oncdoc + "    " + "OCUPADO";
                            t4.Text = label8.Text;
                            t4.BackColor = System.Drawing.Color.FromArgb(255, 128, 128); break;

                        case 12:

                            label9.Text = doctre + "    " + "OCUPADO";
                            t5.Text = label9.Text;
                            t5.BackColor = System.Drawing.Color.FromArgb(255, 128, 128); break;

                        case 13:

                            label10.Text = trecat + "    " + "OCUPADO";
                            t6.Text = label10.Text;
                            t6.BackColor = System.Drawing.Color.FromArgb(255, 128, 128); break;


                        case 16:

                            label11.Text = dsedsie + "    " + "OCUPADO";
                            t7.Text = label11.Text;
                            t7.BackColor = System.Drawing.Color.FromArgb(255, 128, 128); break;

                        case 17:

                            label12.Text = dsiedsei + "    " + "OCUPADO";
                            t8.Text = label12.Text;
                            t8.BackColor = System.Drawing.Color.FromArgb(255, 128, 128); break;

                        case 18:

                            label13.Text = dseidnue + "    " + "OCUPADO";
                            t9.Text = label13.Text;
                            t9.BackColor = System.Drawing.Color.FromArgb(255, 128, 128); break;

                        case 19:

                            label14.Text = dnuevei + "    " + "OCUPADO";
                            t10.Text = label14.Text;
                            t10.BackColor = System.Drawing.Color.FromArgb(255, 128, 128); break;



                    }

                }

            }
            else //no hay NINGUNA cita ese dia
            {

                label5.Text = ochnue + "    " + "LIBRE";
                label6.Text = nuedie + "    " + "LIBRE";
                label7.Text = dieonc + "    " + "LIBRE";
                label8.Text = oncdoc + "    " + "LIBRE";
                label9.Text = doctre + "    " + "LIBRE";
                label10.Text = trecat + "    " + "LIBRE";
                label11.Text = dsedsie + "    " + "LIBRE";
                label12.Text = dsiedsei + "    " + "LIBRE";
                label13.Text = dseidnue + "    " + "LIBRE";
                label14.Text = dnuevei + "    " + "LIBRE";
            }
        }

        private void label5_Click(object sender, EventArgs e)
        {
            string tmp = ochnue + "    " + "OCUPADO";
            DateTime horaCita;
            DateTime horaCitaLabel;
            label4.Text = ochnue;
            btnBuscarCliente.Focus();

            t1.BorderStyle = BorderStyle.FixedSingle;
            t2.BorderStyle = BorderStyle.None;
            t3.BorderStyle = BorderStyle.None;
            t4.BorderStyle = BorderStyle.None;
            t5.BorderStyle = BorderStyle.None;
            t6.BorderStyle = BorderStyle.None;
            t7.BorderStyle = BorderStyle.None;
            t8.BorderStyle = BorderStyle.None;
            t9.BorderStyle = BorderStyle.None;
            t10.BorderStyle = BorderStyle.None;

            string tmpS = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
            horaCitaLabel = DateTime.Parse(tmpS);


            if (label5.Text == tmp)//esta ocupado
            {
                pictureBox1.Image = uacars.Properties.Resources.mal;
                t1.BackColor = System.Drawing.Color.FromArgb(255, 128, 128);

                foreach (DataRow row in dtGlobal.Rows)
                {

                    DateTime dt2 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));
                    DateTime dt1 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[1].ToString()));
                    string h = dt2.Hour.ToString() + ":00:00";

                    if (h == "8:00:00")
                    {

                        idGlobal = Convert.ToInt32(row[0]);
                        tbxCliente.Text = row[4].ToString();
                        tbxEmpleado.Text = row[3].ToString();
                        textMotivo.Text = row[5].ToString();


                    }


                }
            }

            else
            {
                pictureBox1.Image = uacars.Properties.Resources.ok;
                t1.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
                textMotivo.Text = "";
                tbxCliente.Text = "";
                tbxEmpleado.Text = "";
            }


        }



        private void label6_Click(object sender, EventArgs e)
        {
            string tmp = nuedie + "    " + "OCUPADO";
            DateTime horaCita;
            DateTime horaCitaLabel;
            label4.Text = nuedie;
            btnBuscarCliente.Focus();
            t1.BorderStyle = BorderStyle.None;
            t2.BorderStyle = BorderStyle.FixedSingle;
            t3.BorderStyle = BorderStyle.None;
            t4.BorderStyle = BorderStyle.None;
            t5.BorderStyle = BorderStyle.None;
            t6.BorderStyle = BorderStyle.None;
            t7.BorderStyle = BorderStyle.None;
            t8.BorderStyle = BorderStyle.None;
            t9.BorderStyle = BorderStyle.None;
            t10.BorderStyle = BorderStyle.None;

            string tmpS = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
            horaCitaLabel = DateTime.Parse(tmpS);


            if (label6.Text == tmp)//esta ocupado
            {

                pictureBox1.Image = uacars.Properties.Resources.mal;
                t2.BackColor = System.Drawing.Color.FromArgb(255, 128, 128);
                foreach (DataRow row in dtGlobal.Rows)
                {

                    DateTime dt2 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));
                    DateTime dt1 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[1].ToString()));
                    string h = dt2.Hour.ToString() + ":00:00";

                    if (h == "9:00:00")
                    {
                        idGlobal = Convert.ToInt32(row[0]);
                        tbxCliente.Text = row[4].ToString();
                        tbxEmpleado.Text = row[3].ToString();
                        textMotivo.Text = row[5].ToString();

                    }

                }
            }
            else
            {
                pictureBox1.Image = uacars.Properties.Resources.ok;
                t2.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
                textMotivo.Text = "";
                tbxCliente.Text = "";
                tbxEmpleado.Text = "";
            }
        }
        private void label7_Click(object sender, EventArgs e)
        {
            string tmp = dieonc + "    " + "OCUPADO";
            DateTime horaCita;
            DateTime horaCitaLabel;
            label4.Text = dieonc;
            btnBuscarCliente.Focus();

            t1.BorderStyle = BorderStyle.None;
            t2.BorderStyle = BorderStyle.None;
            t3.BorderStyle = BorderStyle.FixedSingle;
            t4.BorderStyle = BorderStyle.None;
            t5.BorderStyle = BorderStyle.None;
            t6.BorderStyle = BorderStyle.None;
            t7.BorderStyle = BorderStyle.None;
            t8.BorderStyle = BorderStyle.None;
            t9.BorderStyle = BorderStyle.None;
            t10.BorderStyle = BorderStyle.None;

            string tmpS = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
            horaCitaLabel = DateTime.Parse(tmpS);


            if (label7.Text == tmp)//esta ocupado
            {


                pictureBox1.Image = uacars.Properties.Resources.mal;
                t3.BackColor = System.Drawing.Color.FromArgb(255, 128, 128);
                foreach (DataRow row in dtGlobal.Rows)
                {

                    DateTime dt2 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));
                    DateTime dt1 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[1].ToString()));
                    string h = dt2.Hour.ToString() + ":00:00";

                    if (h == "10:00:00")
                    {
                        idGlobal = Convert.ToInt32(row[0]);
                        tbxCliente.Text = row[4].ToString();
                        tbxEmpleado.Text = row[3].ToString();
                        textMotivo.Text = row[5].ToString();
                    }

                }
            }
            else
            {
                pictureBox1.Image = uacars.Properties.Resources.ok;
                t3.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
                textMotivo.Text = "";
                tbxCliente.Text = "";
                tbxEmpleado.Text = "";
            }
        }
        private void label8_Click(object sender, EventArgs e)
        {
            string tmp = oncdoc + "    " + "OCUPADO";
            DateTime horaCita;
            DateTime horaCitaLabel;
            label4.Text = oncdoc;
            btnBuscarCliente.Focus();
            t1.BorderStyle = BorderStyle.None;
            t2.BorderStyle = BorderStyle.None;
            t3.BorderStyle = BorderStyle.None;
            t4.BorderStyle = BorderStyle.FixedSingle;
            t5.BorderStyle = BorderStyle.None;
            t6.BorderStyle = BorderStyle.None;
            t7.BorderStyle = BorderStyle.None;
            t8.BorderStyle = BorderStyle.None;
            t9.BorderStyle = BorderStyle.None;
            t10.BorderStyle = BorderStyle.None;

            string tmpS = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
            horaCitaLabel = DateTime.Parse(tmpS);


            if (label8.Text == tmp)//esta ocupado
            {


                pictureBox1.Image = uacars.Properties.Resources.mal;
                t4.BackColor = System.Drawing.Color.FromArgb(255, 128, 128);
                foreach (DataRow row in dtGlobal.Rows)
                {

                    DateTime dt2 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));
                    DateTime dt1 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[1].ToString()));
                    string h = dt2.Hour.ToString() + ":00:00";

                    if (h == "11:00:00")
                    {
                        idGlobal = Convert.ToInt32(row[0]);
                        tbxCliente.Text = row[4].ToString();
                        tbxEmpleado.Text = row[3].ToString();
                        textMotivo.Text = row[5].ToString();
                    }

                }
            }
            else
            {
                pictureBox1.Image = uacars.Properties.Resources.ok;
                t4.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
                textMotivo.Text = "";
                tbxCliente.Text = "";
                tbxEmpleado.Text = "";
            }
        }




        private void label9_Click(object sender, EventArgs e)
        {
            string tmp = doctre + "    " + "OCUPADO";
            DateTime horaCita;
            DateTime horaCitaLabel;
            label4.Text = doctre;
            btnBuscarCliente.Focus();

            t1.BorderStyle = BorderStyle.None;
            t2.BorderStyle = BorderStyle.None;
            t3.BorderStyle = BorderStyle.None;
            t4.BorderStyle = BorderStyle.None;
            t5.BorderStyle = BorderStyle.FixedSingle;
            t6.BorderStyle = BorderStyle.None;
            t7.BorderStyle = BorderStyle.None;
            t8.BorderStyle = BorderStyle.None;
            t9.BorderStyle = BorderStyle.None;
            t10.BorderStyle = BorderStyle.None;

            string tmpS = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
            horaCitaLabel = DateTime.Parse(tmpS);


            if (label9.Text == tmp)//esta ocupado
            {


                pictureBox1.Image = uacars.Properties.Resources.mal;
                t5.BackColor = System.Drawing.Color.FromArgb(255, 128, 128);
                foreach (DataRow row in dtGlobal.Rows)
                {

                    DateTime dt2 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));
                    DateTime dt1 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[1].ToString()));
                    string h = dt2.Hour.ToString() + ":00:00";

                    if (h == "12:00:00")
                    {
                        idGlobal = Convert.ToInt32(row[0]);
                        tbxCliente.Text = row[4].ToString();
                        tbxEmpleado.Text = row[3].ToString();
                        textMotivo.Text = row[5].ToString();
                    }

                }
            }
            else
            {
                pictureBox1.Image = uacars.Properties.Resources.ok;
                t5.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
                textMotivo.Text = "";
                tbxCliente.Text = "";
                tbxEmpleado.Text = "";
            }
        }
        private void label10_Click(object sender, EventArgs e)
        {
            string tmp = trecat + "    " + "OCUPADO";
            DateTime horaCita;
            DateTime horaCitaLabel;
            label4.Text = trecat;
            btnBuscarCliente.Focus();

            t1.BorderStyle = BorderStyle.None;
            t2.BorderStyle = BorderStyle.None;
            t3.BorderStyle = BorderStyle.None;
            t4.BorderStyle = BorderStyle.None;
            t5.BorderStyle = BorderStyle.None;
            t6.BorderStyle = BorderStyle.FixedSingle;
            t7.BorderStyle = BorderStyle.None;
            t8.BorderStyle = BorderStyle.None;
            t9.BorderStyle = BorderStyle.None;
            t10.BorderStyle = BorderStyle.None;

            string tmpS = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
            horaCitaLabel = DateTime.Parse(tmpS);


            if (label10.Text == tmp)//esta ocupado
            {


                pictureBox1.Image = uacars.Properties.Resources.mal;
                t6.BackColor = System.Drawing.Color.FromArgb(255, 128, 128);
                foreach (DataRow row in dtGlobal.Rows)
                {

                    DateTime dt2 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));
                    DateTime dt1 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[1].ToString()));
                    string h = dt2.Hour.ToString() + ":00:00";

                    if (h == "13:00:00")
                    {
                        idGlobal = Convert.ToInt32(row[0]);
                        tbxCliente.Text = row[4].ToString();
                        tbxEmpleado.Text = row[3].ToString();
                        textMotivo.Text = row[5].ToString();
                    }

                }
            }
            else
            {
                pictureBox1.Image = uacars.Properties.Resources.ok;
                t6.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
                textMotivo.Text = "";
                tbxCliente.Text = "";
                tbxEmpleado.Text = "";
            }
        }
        private void label11_Click(object sender, EventArgs e)
        {
            string tmp = dsedsie + "    " + "OCUPADO";
            DateTime horaCita;
            DateTime horaCitaLabel;
            label4.Text = dsedsie;
            btnBuscarCliente.Focus();

            t1.BorderStyle = BorderStyle.None;
            t2.BorderStyle = BorderStyle.None;
            t3.BorderStyle = BorderStyle.None;
            t4.BorderStyle = BorderStyle.None;
            t5.BorderStyle = BorderStyle.None;
            t6.BorderStyle = BorderStyle.None;
            t7.BorderStyle = BorderStyle.FixedSingle;
            t8.BorderStyle = BorderStyle.None;
            t9.BorderStyle = BorderStyle.None;
            t10.BorderStyle = BorderStyle.None;

            string tmpS = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
            horaCitaLabel = DateTime.Parse(tmpS);


            if (label11.Text == tmp)//esta ocupado
            {


                pictureBox1.Image = uacars.Properties.Resources.mal;
                t7.BackColor = System.Drawing.Color.FromArgb(255, 128, 128);
                foreach (DataRow row in dtGlobal.Rows)
                {

                    DateTime dt2 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));
                    DateTime dt1 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[1].ToString()));
                    string h = dt2.Hour.ToString() + ":00:00";

                    if (h == "16:00:00")
                    {
                        idGlobal = Convert.ToInt32(row[0]);
                        tbxCliente.Text = row[4].ToString();
                        tbxEmpleado.Text = row[3].ToString();
                        textMotivo.Text = row[5].ToString();
                    }

                }
            }
            else
            {
                pictureBox1.Image = uacars.Properties.Resources.ok;
                t7.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
                textMotivo.Text = "";
                tbxCliente.Text = "";
                tbxEmpleado.Text = "";
            }
        }


        private void label12_Click(object sender, EventArgs e)
        {
            string tmp = dsiedsei + "    " + "OCUPADO";
            DateTime horaCita;
            DateTime horaCitaLabel;
            label4.Text = dsiedsei;
            btnBuscarCliente.Focus();
            t1.BorderStyle = BorderStyle.None;
            t2.BorderStyle = BorderStyle.None;
            t3.BorderStyle = BorderStyle.None;
            t4.BorderStyle = BorderStyle.None;
            t5.BorderStyle = BorderStyle.None;
            t6.BorderStyle = BorderStyle.None;
            t7.BorderStyle = BorderStyle.None;
            t8.BorderStyle = BorderStyle.FixedSingle;
            t9.BorderStyle = BorderStyle.None;
            t10.BorderStyle = BorderStyle.None;

            string tmpS = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
            horaCitaLabel = DateTime.Parse(tmpS);


            if (label12.Text == tmp)//esta ocupado
            {


                pictureBox1.Image = uacars.Properties.Resources.mal;
                t8.BackColor = System.Drawing.Color.FromArgb(255, 128, 128);
                foreach (DataRow row in dtGlobal.Rows)
                {

                    DateTime dt2 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));
                    DateTime dt1 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[1].ToString()));
                    string h = dt2.Hour.ToString() + ":00:00";

                    if (h == "17:00:00")
                    {
                        idGlobal = Convert.ToInt32(row[0]);
                        tbxCliente.Text = row[4].ToString();
                        tbxEmpleado.Text = row[3].ToString();
                        textMotivo.Text = row[5].ToString();
                    }

                }
            }
            else
            {
                pictureBox1.Image = uacars.Properties.Resources.ok;
                t8.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
                textMotivo.Text = "";
                tbxCliente.Text = "";
                tbxEmpleado.Text = "";
            }
        }
        private void label13_Click(object sender, EventArgs e)
        {
            string tmp = dseidnue + "    " + "OCUPADO";
            DateTime horaCita;
            DateTime horaCitaLabel;
            label4.Text = dseidnue;
            btnBuscarCliente.Focus();

            t1.BorderStyle = BorderStyle.None;
            t2.BorderStyle = BorderStyle.None;
            t3.BorderStyle = BorderStyle.None;
            t4.BorderStyle = BorderStyle.None;
            t5.BorderStyle = BorderStyle.None;
            t6.BorderStyle = BorderStyle.None;
            t7.BorderStyle = BorderStyle.None;
            t8.BorderStyle = BorderStyle.None;
            t9.BorderStyle = BorderStyle.FixedSingle;
            t10.BorderStyle = BorderStyle.None;
            string tmpS = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
            horaCitaLabel = DateTime.Parse(tmpS);


            if (label13.Text == tmp)//esta ocupado
            {


                pictureBox1.Image = uacars.Properties.Resources.mal;
                t9.BackColor = System.Drawing.Color.FromArgb(255, 128, 128);
                foreach (DataRow row in dtGlobal.Rows)
                {

                    DateTime dt2 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));
                    DateTime dt1 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[1].ToString()));
                    string h = dt2.Hour.ToString() + ":00:00";

                    if (h == "18:00:00")
                    {
                        idGlobal = Convert.ToInt32(row[0]);
                        tbxCliente.Text = row[4].ToString();
                        tbxEmpleado.Text = row[3].ToString();
                        textMotivo.Text = row[5].ToString();
                    }

                }
            }
            else
            {
                pictureBox1.Image = uacars.Properties.Resources.ok;
                t9.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
                textMotivo.Text = "";
                tbxCliente.Text = "";
                tbxEmpleado.Text = "";
            }
        }
        private void label14_Click(object sender, EventArgs e)
        {
            string tmp = dnuevei + "    " + "OCUPADO";
            DateTime horaCita;
            DateTime horaCitaLabel;
            label4.Text = dnuevei;
            btnBuscarCliente.Focus();
            t1.BorderStyle = BorderStyle.None;
            t2.BorderStyle = BorderStyle.None;
            t3.BorderStyle = BorderStyle.None;
            t4.BorderStyle = BorderStyle.None;
            t5.BorderStyle = BorderStyle.None;
            t6.BorderStyle = BorderStyle.None;
            t7.BorderStyle = BorderStyle.None;
            t8.BorderStyle = BorderStyle.None;
            t9.BorderStyle = BorderStyle.None;
            t10.BorderStyle = BorderStyle.FixedSingle;

            string tmpS = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
            horaCitaLabel = DateTime.Parse(tmpS);


            if (label14.Text == tmp)//esta ocupado
            {

                pictureBox1.Image = uacars.Properties.Resources.mal;
                t10.BackColor = System.Drawing.Color.FromArgb(255, 128, 128);
                foreach (DataRow row in dtGlobal.Rows)
                {

                    DateTime dt2 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[2].ToString()));
                    DateTime dt1 = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990, 5, 6)).ConvertFrom(row[1].ToString()));
                    string h = dt2.Hour.ToString() + ":00:00";

                    if (h == "19:00:00")
                    {
                        idGlobal = Convert.ToInt32(row[0]);
                        tbxCliente.Text = row[4].ToString();
                        tbxEmpleado.Text = row[3].ToString();
                        textMotivo.Text = row[5].ToString();
                    }

                }
            }
            else
            {
                pictureBox1.Image = uacars.Properties.Resources.ok;
                t10.BackColor = System.Drawing.Color.FromArgb(128, 255, 128);
                textMotivo.Text = "";
                tbxCliente.Text = "";
                tbxEmpleado.Text = "";

            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            dataGridView1.CurrentRow.Selected = true;
            if (dataGridView1.Columns.Count > 3)
            {
                idGlobal = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["id"].Value.ToString());
                tbxCliente.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["cliente"].Value.ToString();
                tbxEmpleado.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["vendedor"].Value.ToString();
                textMotivo.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["descripcion"].Value.ToString();
            }
            //else
            //{
            //dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["fecha"].Value.ToString();
            //}
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1.CurrentRow.Selected = true;
            if (dataGridView1.Columns.Count > 3)
            {
                idGlobal = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["id"].Value.ToString());
                tbxCliente.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["cliente"].Value.ToString();
                tbxEmpleado.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["vendedor"].Value.ToString();
                textMotivo.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["descripcion"].Value.ToString();
            }
        }

        private void buttonModificar_Click(object sender, EventArgs e)
        {
            if (ValidarCampos())
            {

                try
                {

                    string fechaSeleccionada = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
                    string horaBuena = null;

                    if (label4.Text == "8:00-9:00")
                        horaBuena = "8:00:00";
                    if (label4.Text == "9:00-10:00")
                        horaBuena = "9:00:00";
                    if (label4.Text == "10:00-11:00")
                        horaBuena = "10:00:00";
                    if (label4.Text == "11:00-12:00")
                        horaBuena = "11:00:00";
                    if (label4.Text == "12:00-13:00")
                        horaBuena = "12:00:00";
                    if (label4.Text == "13:00-14:00")
                        horaBuena = "13:00:00";
                    if (label4.Text == "16:00-17:00")
                        horaBuena = "16:00:00";
                    if (label4.Text == "17:00-18:00")
                        horaBuena = "17:00:00";
                    if (label4.Text == "18:00-19:00")
                        horaBuena = "18:00:00";
                    if (label4.Text == "19:00-20:00")
                        horaBuena = "19:00:00";



                    string horaSeleccionada = "01/01/1900 " + horaBuena;

                    DateTime fechaSeleccion = new DateTime();
                    fechaSeleccion = DateTime.Parse(fechaSeleccionada);
                    DateTime horaSeleccion = new DateTime();
                    horaSeleccion = DateTime.Parse(horaSeleccionada);

                    DateTime dt = new DateTime();

                    ENCitas actualizado = new ENCitas(idGlobal, fechaSeleccion, horaSeleccion, int.Parse(tbxEmpleado.Text), int.Parse(tbxCliente.Text), textMotivo.Text.ToString(), dt, dt);

                    actualizado.ModificarCita();
                    toolStripStatusLabel1.Text = "Modificación realizada";

                    string fechaSeleccionada1 = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
                    recalcularCitas(DateTime.Parse(fechaSeleccionada1));
                    //dataGridView1.DataSource = actualizado.BuscarCitas();

                    ficheros f = new ficheros();
                    f.EscribirLog(2, 5, idGlobal.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error al modificar la cita.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);

                }



            }
        }

        private void buttonBorrar_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("¿Desea eliminar la cita seleccionada?", "Advertencia", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    //MessageBox.Show(idGlobal.ToString());
                    DateTime dt = new DateTime();
                    ENCitas borrado = new ENCitas(idGlobal, DateTime.Parse("01/01/1900"), DateTime.Parse("01/01/1900 0:00:00"), 0, 0, "null", dt, dt);

                    borrado.BorrarCita();
                    string fechaSeleccionada = monthCalendar1.SelectionRange.Start.Day + "/" + monthCalendar1.SelectionRange.Start.Month + "/" + monthCalendar1.SelectionRange.Start.Year + " 0:00:00";
                    recalcularCitas(DateTime.Parse(fechaSeleccionada));
                    toolStripStatusLabel1.Text = "Baja realizada";

                    ficheros f = new ficheros();
                    f.EscribirLog(1, 5, idGlobal.ToString());
                }

                else
                {
                    toolStripStatusLabel1.Text = "Borrado Cancelado";
                }



            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al borrar la cita.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);

            }

        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            label4.Text = "-------";
            pictureBox1.Image = null;
        }

        private void tbxCliente_TextChanged(object sender, EventArgs e)
        {
            if (tbxCliente.Text.Length == 0 && tbxEmpleado.Text.Length == 0)
            {
                if (buttonBorrar.Enabled == true)
                    buttonBorrar.Enabled = false;

                if (buttonModificar.Enabled == true)
                    buttonModificar.Enabled = false;
                if (buttonNuevo.Enabled == true)
                    buttonNuevo.Enabled = false;
            }
            else
            {
                if (tbxEmpleado.Text.Length > 0 && tbxCliente.Text.Length > 0)
                {
                    if (buttonBorrar.Enabled == false)
                        buttonBorrar.Enabled = true;
                    if (buttonModificar.Enabled == false)
                        buttonModificar.Enabled = true;
                    if (buttonNuevo.Enabled == false)
                        buttonNuevo.Enabled = true;

                }
            }
        }

        private void tbxEmpleado_TextChanged(object sender, EventArgs e)
        {
            if (tbxCliente.Text.Length == 0 && tbxEmpleado.Text.Length == 0)
            {
                if (buttonBorrar.Enabled == true)
                    buttonBorrar.Enabled = false;

                if (buttonModificar.Enabled == true)
                    buttonModificar.Enabled = false;
                if (buttonNuevo.Enabled == true)
                    buttonNuevo.Enabled = false;
            }
            else
            {
                if (tbxEmpleado.Text.Length > 0 && tbxCliente.Text.Length > 0)
                {
                    if (buttonBorrar.Enabled == false)
                        buttonBorrar.Enabled = true;

                    if (buttonModificar.Enabled == false)
                        buttonModificar.Enabled = true;

                    if (buttonNuevo.Enabled == false)
                        buttonNuevo.Enabled = true;

                }
            }
        }

        private void btnBuscarCliente_Click(object sender, EventArgs e)
        {
            frmClientes cliente = new frmClientes("");
            cliente.pedC = this;
            cliente.MdiParent = this.MdiParent;
            cliente.Show();
        }

        private void btnBuscarEmpleado_Click(object sender, EventArgs e)
        {
            frmEmpleados empleado = new frmEmpleados("empleado");
            empleado.pedC = this;
            empleado.MdiParent = this.MdiParent;
            empleado.Show();
        }

        private void label4_TextChanged(object sender, EventArgs e)
        {
            if (label4.Text == "-------")
            {
                if (btnBuscarCliente.Enabled == true)
                    btnBuscarCliente.Enabled = false;
                if (btnBuscarEmpleado.Enabled == true)
                    btnBuscarEmpleado.Enabled = false;
            }
            else
            {
                if (btnBuscarCliente.Enabled == false)
                    btnBuscarCliente.Enabled = true;
                if (btnBuscarEmpleado.Enabled == false)
                    btnBuscarEmpleado.Enabled = true;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            ImprimirTabla();
        }

        public void ImprimirTabla()
        {
            frmImprimir Imprimir = new frmImprimir(dataGridView1, "CITAS");
            Imprimir.Show();
        }

        private void buttonXML_Click(object sender, EventArgs e)
        {
            ExportarXML();
        }

        public void ExportarXML()
        {
            saveFileDialog1.FileName = "UACARS citas.xml";
            saveFileDialog1.Filter = "Archivo XML|*.xml";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string path = saveFileDialog1.FileName;
                int numFilas = 0;

                numFilas = dataGridView1.Rows.Count;

                DataSet ds = new DataSet("Citas");
                DataTable dt = new DataTable("Cita");

                dt.Columns.Add("id", Type.GetType("System.String"));

                dt.Columns.Add("fecha", Type.GetType("System.String"));

                dt.Columns.Add("hora", Type.GetType("System.String"));

                dt.Columns.Add("vendedor", Type.GetType("System.String"));

                dt.Columns.Add("cliente", Type.GetType("System.String"));

                dt.Columns.Add("descripcion", Type.GetType("System.String"));

                if (numFilas > 0)
                {
                    for (int i = 0; i < numFilas; i++)
                    {
                        DataRow dr;
                        dr = dt.NewRow();

                        dr.BeginEdit();

                        dr["id"] = dataGridView1[0, i].Value.ToString();

                        dr["fecha"] = dataGridView1[1, i].Value.ToString();

                        dr["hora"] = dataGridView1[2, i].Value.ToString();

                        dr["vendedor"] = dataGridView1[3, i].Value.ToString();

                        dr["cliente"] = dataGridView1[4, i].Value.ToString();

                        dr["descripcion"] = dataGridView1[5, i].Value.ToString();

                        dr.EndEdit();

                        dt.Rows.Add(dr);
                    }
                }
                ds.Tables.Add(dt);
                ds.WriteXml(path);
            }
        }

        private void buttonPDF_Click(object sender, EventArgs e)
        {
            PDF pdf = new PDF();
            pdf.generarPDF("frmCitas", saveFileDialog1, dataGridView1);
        }

        public void ImportarXML()
        {
            DataSet dataSet1 = new DataSet();
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "Archivo XML|*.xml";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string file = openFileDialog1.FileName;

                if (file.Length > 0)
                {
                    try
                    {
                        dataSet1.ReadXml(file);

                        DataGridView aux = new DataGridView();
                        dataGridView1.DataSource = aux.DataSource;
                        dataGridView1.DataMember = "";

                        dataGridView1.DataSource = dataSet1;
                        dataGridView1.DataMember = "cita";

                        System.IO.StringWriter swXML = new System.IO.StringWriter();
                        dataSet1.WriteXmlSchema(swXML);
                    }

                    catch (System.Security.SecurityException ex)
                    {
                        MessageBox.Show("Error al acceder al fichero," + ex.Message, "Error de fichero", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                }
            }
        }

        private void buttonExcel_Click(object sender, EventArgs e)
        {
            Excel();
        }

        public void Excel()
        {
            XML xml = new XML();
            xml.ExportarExcel(dataGridView1);
        }
    }
}
